var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeIndex: 0,
    navList: [{
      name: '正在阅读',
      _id: '0',
    },
    {
      name: '历史阅读',
      _id: '1',
    },
    {
      name: '我的收藏',
      _id: '2',
    }],

    prompt: {
      hidden: !0,
      icon: '../../assets/images/iconfont-order-default.png',
      title: '暂无相关书籍',
      text: '没有教师推荐的数据，可以看看家长推荐的优秀书籍',
      gurl: '../../pages/home/home',
      gtype: "switchTab"
    },
   
    tjimagedefault: app.myUlife.ulifeUrl + 'testimage/timg1.jpg',//默认教师推荐图片

    //正在阅读
    zayd:{
      page: 1,
      limit: 20,
      totalpage: 0,
      datas:[]
    },

    //历史阅读
    lsyd: {
      page: 1,
      limit: 20,
      totalpage: 0,
      datas: []
    },

    //我的收藏
    wdsc: {
      page: 1,
      limit: 20,
      totalpage: 0,
      datas: []
    },

    usid: '',
  },

  getZayd() {
    var that = this;
    var usid = that.data.usid;
    var page = that.data.zayd.page;
    var limit = that.data.zayd.limit;
    var totalpage = that.data.zayd.totalpage;
    if (page != 1) {
      if (page > totalpage) {
        return;
      }
    }
    app.wxget({
      url: 'wdsj/getZayd.html',
      data: {
        usid: usid,
        page: page,
        limit: limit
      },
      success: function (res) {
        var datas = res.data.list;
        var totalpage = res.data.totalpage;
        var odatas = that.data.zayd.datas;
        for (var i = 0; i < datas.length; i++) {
          odatas.push(datas[i]);
        }
        var zayd = that.data.zayd;
        zayd.datas = odatas;
        zayd.totalpage = totalpage;
        that.setData({
          zayd: zayd
        })
      }
    })
  },


  getLsyd() {
    var that = this;
    var usid = that.data.usid;
    var page = that.data.lsyd.page;
    var limit = that.data.lsyd.limit;
    var totalpage = that.data.lsyd.totalpage;
    if (page != 1) {
      if (page > totalpage) {
        return;
      }
    }
    app.wxget({
      url: 'wdsj/getLsyd.html',
      data: {
        usid: usid,
        page: page,
        limit: limit
      },
      success: function (res) {
        var datas = res.data.list;
        var totalpage = res.data.totalpage;
        var odatas = that.data.lsyd.datas;
        for (var i = 0; i < datas.length; i++) {
          odatas.push(datas[i]);
        }
        var lsyd = that.data.lsyd;
        lsyd.datas = odatas;
        lsyd.totalpage = totalpage;
        that.setData({
          lsyd: lsyd,
        })
      }
    })
  },


  getCollection() {
    var that = this;
    var usid = that.data.usid;
    var page = that.data.wdsc.page;
    var limit = that.data.wdsc.limit;
    var totalpage = that.data.wdsc.totalpage;
    if (page != 1) {
      if (page > totalpage) {
        return;
      }
    }
    app.wxget({
      url: 'wdsj/getCollection.html',
      data: {
        usid: usid,
        page: page,
        limit: limit
      },
      success: function (res) {
        var datas = res.data.list;
        var totalpage = res.data.totalpage;
        var odatas = that.data.wdsc.datas;
        for (var i = 0; i < datas.length; i++) {
          odatas.push(datas[i]);
        }
        var wdsc = that.data.wdsc;
        wdsc.datas = odatas;
        wdsc.totalpage = totalpage;
        that.setData({
          wdsc: wdsc
        })
      }
    })
  },

  errormoretj: function (e) {
    if (e.type == "error") {
      var errorImgIndex = e.target.dataset.errorimg
      var zayd = this.data.zayd;
      var datas = zayd.datas;  　　　　　　　//将图片列表数据绑定到变量
        datas[errorImgIndex].FM = this.data.tjimagedefault; //错误图片替换为默认图片
      this.setData({
        zayd: zayd
      })
    }
  },
  errormorewc: function (e) {
    if (e.type == "error") {
      var errorImgIndex = e.target.dataset.errorimg
      var lsyd = this.data.lsyd;
      var datas = lsyd.datas;  　　　　　　　//将图片列表数据绑定到变量
        datas[errorImgIndex].FM = this.data.tjimagedefault //错误图片替换为默认图片
      this.setData({
        lsyd: lsyd
      })
    }
  },

  errormoresc: function (e) {
    if (e.type == "error") {
      var errorImgIndex = e.target.dataset.errorimg
      var wdsc = this.data.wdsc;
      var datas = wdsc.datas;  　　　　　　　//将图片列表数据绑定到变量
        datas[errorImgIndex].FM = this.data.tjimagedefault //错误图片替换为默认图片
      this.setData({
        wdsc: wdsc
      })
    }
  },


  //切换
  onTapTag(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.setData({
      activeIndex: index,
    })
  },




  //上拉刷新
  tolower() {
    var that = this;
    var index = that.data.activeIndex;
    //正在阅读
    if (index==0){
      var zayd = that.data.zayd;
      var page = parseInt(zayd.page) + 1;
      zayd.page = page;
      this.setData({
        zayd: zayd
      })
      this.getZayd();
    } else if (index == 1){
      var lsyd = that.data.lsyd;
      var page = parseInt(lsyd.page) + 1;
      lsyd.page = page;
      this.setData({
        lsyd: lsyd
      })
      //历史阅读
      this.getLsyd();
    } else if (index == 2) {
      var wdsc = that.data.wdsc;
      var page = parseInt(that.data.wdsc.page) + 1;
      wdsc.page = page;
      this.setData({
        wdsc: wdsc
      })
      this.getCollection();
    }
  },


  todetail(e) {
    var bid = e.currentTarget.dataset.bid;
    wx.navigateTo({
      url: '../../pages/bookDetaildk/bookDetaildk?bid=' + bid
    })
  },
 
  todetailYx(e) {
    var bid = e.currentTarget.dataset.bid;
    wx.navigateTo({
      url: '../../pages/bookDetail/bookDetail?bid=' + bid
    })
  },

  onShow: function (options) {
    var usid = wx.getStorageSync('usid');
    if (!usid) {
      wx.reLaunch({
        url: '../../pages/login/login'
      })
      return;
    }
    this.setData({
      usid: usid,
      zayd: {
        page: 1,
        limit: 20,
        totalpage: 0,
        datas: []
      },
      lsyd: {
        page: 1,
        limit: 20,
        totalpage: 0,
        datas: []
      },
      wdsc: {
        page: 1,
        limit: 20,
        totalpage: 0,
        datas: []
      },
    })

   
    this.getZayd();
    this.getLsyd();
    this.getCollection();
  }
})